<template>
  <div class="dashboard-container">
    <div class="dashboard-editor-container">
      <panel-group :statistics="model"/>
      <el-row :gutter="40">
        <el-col :span="18">
          <div class="chart-wrapper">
            <map-chart :statistics="model"></map-chart>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="chart-wrapper">
            <bar-chart :statistics="model"/>
          </div>
        </el-col>
      </el-row>
      <div class="dis-table-wrap">
        <div class="dis-title">故障网关列表</div>
        <el-table
          :key="tableKey"
          :data="model.faultGateways"
          class="trans-table"
          border
          fit
          highlight-current-row
          style="width: 100%;"
        >
          <el-table-column label="网关编码" prop="deviceCode" align="center" width="180">
            <template slot-scope="{row}">
              <el-link type="primary" @click="goToRemote(row)">{{ row.deviceCode}}</el-link>
            </template>
          </el-table-column>
          <el-table-column label="公司名称" prop="company" align="center" min-width="90"></el-table-column>
          <el-table-column label="地址" align="center" min-width="90">
            <template slot-scope="{row}">
              {{ row.province || '' }}
              {{ row.city || '' }}
              {{ row.region || '' }}
            </template>
          </el-table-column>
          <el-table-column label="网关状态" align="center" min-width="90">
            <template slot-scope="{row}">
              <i v-if="row.status === 0" title="未知" class="square-status" style="background-color: #909399"></i>
              <i v-else-if="row.status === 1" title="运行中" class="square-status" style="background-color: #67C23A"></i>
              <i v-else-if="row.status === 2" title="故障" class="square-status" style="background-color: #F56C6C"></i>
              <i v-else-if="row.status === 3" title="已停用" class="square-status" style="background-color: #E6A23C"></i>
              <i v-else-if="row.status === 4" title="备机启用中" class="square-status" style="background-color: #409EFF"></i>
              <i v-else-if="row.status === 5" title="备机备用中" class="square-status" style="background-color: #66b1ff"></i>
              <span v-if="row.status===0">未知</span>
              <span v-if="row.status===1">运行中</span>
              <span v-if="row.status===2">故障</span>
              <span v-if="row.status===3">已停用</span>
              <span v-if="row.status===4">备机启用中</span>
              <span v-if="row.status===5">备机备用中</span>
            </template>
          </el-table-column>
          <el-table-column label="连接状态" align="center" min-width="90">
            <template slot-scope="{row}">
              <i v-if="row.managerConnectionStatus === 0" title="未知" class="el-icon-remove" style="color: #909399"></i>
              <i v-if="row.managerConnectionStatus === 1" title="连接" class="el-icon-success" style="color: #67C23A"></i>
              <i v-if="row.managerConnectionStatus === 2" title="断开" class="el-icon-error" style="color: #F56C6C"></i>
              <span v-if="row.managerConnectionStatus === 0">未知</span>
              <span v-if="row.managerConnectionStatus === 1">连接</span>
              <span v-if="row.managerConnectionStatus === 2">断开</span>
            </template>
          </el-table-column>
          <el-table-column label="最近一次更新时间" align="center" min-width="120">
            <template slot-scope="{row}">
              <span>{{ row.latUpdateTime | parseTime("{y}-{m}-{d} {h}:{i}:{s}") }}</span>
            </template>
          </el-table-column>

        </el-table>
      </div>

    </div>
  </div>
</template>

<script>
  import { getDashboard } from "@/api/dashboard";
  import PanelGroup from "./components/PanelGroup";
  import BarChart from "./components/BarChart";
  import MapChart from "./components/mapChart";

  export default {
    name: "Dashboard",
    components: {
      PanelGroup,
      BarChart,
      MapChart
    },
    data() {
      return {
        isSubmitLoading: false,
        tableKey: 0,
        model: {},
        timer: undefined
      };
    },
    created() {
      if (this.$store.getters.user.lastPasswordResetTime == null && this.$store.state.session.firstLogin) {
        this.$confirm('您正在使用默认密码，请修改密码!', '请修改密码', {
          confirmButtonText: '去修改',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$router.push({
            name: 'resetPassword'
          })
        }).catch(() => {
          this.$store.state.session.firstLogin = false
        });
        this.$store.state.session.firstLogin = false
      }
      this.load();
    },
    destroyed() {
      clearInterval(this.timer);
    },
    methods: {
      load() {
        this.getList();
        this.setInterval();
      },
      setInterval() {
        this.timer = setInterval(() => {
          this.getList();
        }, 3000);
      },
      getList() {
        this.isSubmitLoading = true;
        getDashboard().then(result => {
          this.model = result;
          this.isSubmitLoading = false;
        });
      },
      transStatus(status) {
        let returnVal = '';
        switch (status) {
          case 0:
            returnVal = '未配置';
            break;
          case 1:
            returnVal = '运行中';
            break;
          case 2:
            returnVal = '运行检测失败';
            break;
          case 3:
            returnVal = '已停止';
            break;
          case 4:
            returnVal = '启动失败';
            break;
          case 5:
            returnVal = '等待中';
            break;
          case 6:
            returnVal = '启动中';
            break;
          case 7:
            returnVal = '停止中';
            break;
          case 8:
            returnVal = '停止失败';
            break;
        }
        return returnVal;
      },
      transDataStatus(status) {
        let returnVal = '';
        switch (status) {
          case 0:
            returnVal = '正常';
            break;
          case 1:
            returnVal = '无物理端口';
            break;
          case 2:
            returnVal = '无数据源';
            break;
          case 4:
            returnVal = '无数据点';
            break;
          case 8:
            returnVal = '无本地串口地址(COM)/本地IP地址(TCP)';
            break;
          case 16:
            returnVal = '无本地端口(TCP)';
            break;
          case 32:
            returnVal = '本地串口地址无效(COM)/本地IP地址无效(TCP)';
            break;
          case 64:
            returnVal = '数据读取超时';
            break;
        }
        return returnVal;
      },
      goToRemote(row) {
        this.$router.push({
          name: 'remote',
          query: {
            id: row.id,
            managerConnectionStatus: row.managerConnectionStatus,
            deviceCode: row.deviceCode + ':' + row.company
          }
        });
      }
    }
  };
</script>
<style lang="scss" scoped>
  .dashboard-editor-container {
    padding: 32px;
    background-color: rgb(240, 242, 245);
    position: relative;

    .github-corner {
      position: absolute;
      top: 0;
      border: 0;
      right: 0;
    }

    .chart-wrapper {
      background: #fff;
      padding: 16px;
      margin-bottom: 32px;
    }
  }

  .dis-table-wrap {
    padding: 0 30px 30px;
    background-color: #fff;

    & + .dis-table-wrap {
      margin-top: 32px;
    }
  }

  .dis-title {
    display: flex;
    padding: 20px 0;
    font-weight: bold;

    &:before {
      content: '';
      width: 5px;
      height: 18px;
      background-color: #409EFF;
      margin-right: 10px;
    }
  }

  .dis-table {
    width: 100%;
    border-collapse: collapse;

    tr {
      th {
        width: 140px;
        font-size: 14px;
        text-align: left;
        background: #eef1f6;
      }

      td {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: normal;
        word-break: break-all;
        line-height: 23px;
        padding-left: 10px;
        padding-right: 10px;
        font-size: 14px;
        color: #606266;
      }

      th, td {
        border: 1px solid #dfe6ec;
        padding: 8px;
        vertical-align: middle;
      }
    }
  }

  .trans-table {
    tr {
      th {
        background: #eef1f6;
        color: #000000;
      }
    }
  }
  .square-status{
    display: inline-block;
    line-height: 1;
    width: 10px;
    height: 10px;
  }

</style>
